using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KVB.Common;

namespace Customer_Profile.Admin
{
    public class CodeTypeDao
    {
        public CodeTypeDao()
        {
        }


        internal int Add(string Name, string Remark, string order, string isuse)
        {
            SqlConnection conn = null;
            DataSet ds = new DataSet();
            try
            {
                DB.Connection(ref conn);
                SqlParameter[] parameters = { new SqlParameter("@CodeTypeName",SqlDbType.NVarChar,50),
                                              new SqlParameter("@CodeTypeRemark",SqlDbType.NVarChar,50),
                                              new SqlParameter("@CodeTypeOrder",SqlDbType.Int),
                                              new SqlParameter("@Y_N",SqlDbType.VarChar,10)
                };
                parameters[0].Value = Name;
                parameters[1].Value = Remark;
                parameters[2].Value = order;
                parameters[3].Value = isuse;
                ds = DB.ExecuteDataset(conn, CommandType.StoredProcedure, "PROC_CODETYPEADD", parameters);
                if (ds.Tables.Count > 0)
                    return int.Parse(ds.Tables[0].Rows[0][0].ToString());
                else
                    return -1;
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("CodeTypeDaoADD", ex.ToString());
                return -1;
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
        }

        internal DataSet GetModel(string id)
        {
            SqlConnection conn = null;            
            try
            {
                string sql = "select * from dbo.TP_TType where id = "+id;
                DB.Connection(ref conn);
                return DB.ExecuteDataset(conn, CommandType.Text, sql);
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("CodeTypeDaoGetModel", ex.ToString());
                return null;
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
        }

        internal DataSet GetList()
        {
            SqlConnection conn = null;
            try
            {
                string sql = "select * from TP_TType where Y_N = 'Y' order by CodeTypeOrder";
                DB.Connection(ref conn);
                return DB.ExecuteDataset(conn, CommandType.Text, sql);
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("", ex.ToString());
                return null;
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
        }

        internal void Modify(string id, string Name, string Remark, string order, string isuse)
        {
            SqlConnection conn = null;
            DataSet ds = new DataSet();
            try
            {
                DB.Connection(ref conn);
                string sql = "update TP_TType set CodeTypeName = N'" + Name + "',CodeTypeRemark = N'" + Remark + "',CodeTypeOrder = '" + order + "',Y_N='" + isuse + "' where ID=" + id;
                DB.ExecuteNonQuery(conn, CommandType.Text, sql);
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("CodeTypeDaoADD", ex.ToString());                
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
        }

        internal bool IsType(string id)
        {
            SqlConnection conn = null;
            DataSet ds = new DataSet();
            try
            {
                string sql = "select count(1) from TP_TType where Y_N = 'Y' and ID='" + id+"'";
                DB.Connection(ref conn);
                ds = DB.ExecuteDataset(conn, CommandType.Text, sql);
                if (int.Parse(ds.Tables[0].Rows[0][0].ToString()) > 0)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("IsType", ex.ToString());
                return false;
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
        }
    }
}